package forum.communication;

import forum.server.controller.ForumFacadeInt;
import forum.server.domain.PostContent;
import forum.server.exceptions.*;

/**
 * @author Tamir 
 *
 */
public class AddReplyMessage extends ClientMessage {

	private static final long serialVersionUID = 6721172261483674344L;
	
	/**
	 * The id of the message to which the reply message is added to.
	 * (The forum is nested).
	 */
	private int parentMessageId;
	
	/**
	 * The header of the reply message.
	 */
	private String header;
	/**
	 * The content of the reply message.
	 */
	private PostContent content;
	
	

	public AddReplyMessage(int parentMessageId, String header, PostContent content) {
		this.parentMessageId = parentMessageId;
		this.header = header;
		this.content = content;
	}

	/* (non-Javadoc)
	 * @see forum.tcpcommunicationlayer.ClientMessage#doOperation(forum.server.domainlayer.ForumFacade)
	 */
	@Override
	public ServerResponse doOperation(ForumFacadeInt forum) {
		try {
			forum.replyToPost(parentMessageId, header, content);
			return new ServerResponse("The reply to message "+parentMessageId+ " was successfully posted.", true);
		} catch (NotLoggedInException e) {
			return new ServerResponse("Guests are not allowed to reply to posts. Please login.", false);
			
		} catch (NoSuchPostException e) {
			return new ServerResponse("The post "+parentMessageId+" does not exist.", false);		
		} catch (DatabaseException e) {
			return new ServerResponse("The server is currently down for maintanance.\nPlease try again later.",false);
		}
	}

}
